//get请求封装
function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: url,
			data: postData,
			method: "GET",
			dataType: 'json',
			header: {
				'content-type': 'application/json'
			},
			success: function(res) {
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}

function getRequestS(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: url,
			data: postData,
			method: "GET",
			dataType: 'json',
			header: {
				'content-type': 'application/json',
				"accessToken": uni.getStorageSync('headerToken').accessToken,
				"projectId": uni.getStorageSync('headerToken').projectId,
			},
			success: function(res) {
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}




// （其实这里你不用var 可以直接return 只是我在写的时候为了方便自己看的习惯）
//post请求封装
function postRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: url,
			data: postData,
			method: 'POST',
			header: {
				'content-type': 'application/json'
				// 'content-type': 'application/x-www-form-urlencoded'
			},
			// header:
			// 	{
			// 		'content-type':'application/json',
			// 		'Authorization':'Bearer '+ postHeader  //后台传给你的token把它值存在这里
			// 		},
			success: function(res) {
				if (res.statusCode === 200 && res.data.resultCode == 0) {
					resolve(res.data);
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}

function postRequestS(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: url,
			data: postData,
			method: 'POST',
			header: {
				'content-type': 'application/json',
				"accessToken": uni.getStorageSync('headerToken').accessToken,
				"projectId": uni.getStorageSync('headerToken').projectId,
			},
			success: function(res) {
				if (res.statusCode === 200 && res.data.resultCode == 0) {
					resolve(res.data);
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}

function fileRequest(url, data, path) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		//开始上传，此处可先注释看压缩效果
		uni.uploadFile({
			url: url,
			method: "POST",
			formData: postData,
			filePath: path,
			name: 'file',
			success: function(res) {
				if (res.statusCode === 200 && res.data.resultCode == 0) {
					resolve(res.data);
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}

		})

	});
	return promise;
}

// var upload="http://1.14.19.236:8052/inspectionTestScreen/App/upload/uploadFile.do"
module.exports = {
	postRequest,
	postRequestS, //带header
	getRequest,
	getRequestS, //带header
	fileRequest,
	// upload
}
